package com.winit.common.base.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wangjian on 16/1/17.
 */
public class BaseController {

    static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    public static final String REQUEST_SUCCESS = "0";

    public static final String REQUEST_ERROR = "1";

    public static final String REQUEST_EXP = "4";

    public static final String REQUEST_SUCCESS_MSG = "success!";

    @ExceptionHandler
    public Map<String, Object> exp(HttpServletRequest request, Exception ex) {
        logger.error("系统异常", ex);
        Map<String, Object> rtnMap = new HashMap<String, Object>();
        rtnMap.put("code", "4");
        rtnMap.put("desc", "系统异常,请联系系统管理员!" + ex.getMessage());
        return rtnMap;
    }

    public Map getRtn(String code, String desc) {
        Map rtnMap = new HashMap();
        rtnMap.put("code", code);
        rtnMap.put("desc", desc);
        return rtnMap;
    }

    public <T> Map getRtn(String code, String desc, T data) {
        Map rtnMap = new HashMap();
        rtnMap.put("code", code);
        rtnMap.put("desc", desc);
        rtnMap.put("data", data);
        return rtnMap;
    }

}
